package com.ljy.mallCommonApi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljy.mallCommonApi.common.api.CommonResult;
import com.ljy.mallCommonApi.entity.PmsBrand;
import com.ljy.mallCommonApi.service.IPmsBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author ljy
 * @since 2022-01-02
 */
@RestController
@Slf4j
@RequestMapping("/brand")
public class PmsBrandController {
    @Autowired
    IPmsBrandService pmsBrandService;

    @PreAuthorize("hasAuthority('pms:brand:read')")
    @GetMapping( "/listAll")
    public CommonResult<List<PmsBrand>> getBrandList() {
        return CommonResult.success(pmsBrandService.list());
    }

    @PostMapping( "/create")
    public CommonResult createBrand(@RequestBody PmsBrand pmsBrand) {
        CommonResult commonResult;
        boolean save_b = pmsBrandService.save(pmsBrand);
        if (save_b) {
            commonResult = CommonResult.success("插入成功");
            log.info("插入成功:{}", pmsBrand);
        } else {
            commonResult = CommonResult.failed("插入失败");
            log.info("插入失败:{}", pmsBrand);
        }
        return commonResult;
    }

    @PostMapping( "/update/{id}")
    public CommonResult<PmsBrand> updateBrand(@PathVariable("id") Long id,
                                              @RequestBody PmsBrand pmsBrandDto) {
        CommonResult commonResult;
        pmsBrandDto.setId(id);
        boolean updateById_b = pmsBrandService.updateById(pmsBrandDto);
        if (updateById_b) {
            commonResult = CommonResult.success("更新成功");
            log.info("更新成功:{}", pmsBrandDto);
        } else {
            commonResult = CommonResult.failed("更新失败");
            log.info("更新失败:{}", pmsBrandDto);
        }
        return commonResult;
    }

    @GetMapping( "/delete/{id}")
    public CommonResult<PmsBrand> deleteBrand(@PathVariable("id") Long id) {
        boolean removeById = pmsBrandService.removeById(id);
        CommonResult commonResult;
        if (removeById){
            commonResult = CommonResult.success("删除成功");
            log.info("删除成功:id：{}", removeById);
        } else {
            commonResult = CommonResult.failed("删除失败");
            log.info("删除失败:id：{}", removeById);
        }
        return commonResult;
    }

    @GetMapping( "/list")
    public CommonResult<List<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        Page<PmsBrand> page=new Page<>(pageNum,pageSize);

        page = pmsBrandService.page(page);
        List<PmsBrand> records = page.getRecords();
        return CommonResult.success(records);
    }

    @GetMapping("/{id}")
    public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
        PmsBrand pmsBrandServiceOne = pmsBrandService.getOne(new QueryWrapper<PmsBrand>().eq("id", id));
        return CommonResult.success(pmsBrandServiceOne,"查询成功");
    }
}
